﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using DA;
using System.Data;


namespace WebDemo_ADO.WUC
{
    public partial class ViewCart : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MyList list = (MyList)Session["GioHang"];
                GridView1.DataSource = list;
                GridView1.DataBind();

                //GridView2.DataSource = list;
                //GridView2.DataBind();
            }
        }

      
        protected void btnThanhToan_Click(object sender, EventArgs e)
        {
            //MyList list = (MyList)Session["GioHang"];
            //HoaDondb db = new HoaDondb();
            //HoaDonData data = new HoaDonData();
            //data.MaTV = 1;
            //data.DiaChi = "HCM";
            //data.NhanVien = -1;
            //if (Session["GioHang"] != null)
            //{
            //    if (db.Insert(data))
            //    {
            //        data.MaHD = db.getOneHD(15).MaHD;
            //        CTHoaDonData cthdData = new CTHoaDonData();
            //        foreach (CartItem item in list)
            //        {
            //            CTHonDonDB kk = new CTHonDonDB();
            //            cthdData.MaHD = data.MaHD;
            //            cthdData.MaSP = item.ProductID;
            //            cthdData.Sl = item.Quantity;
            //            cthdData.DonGia = item.Price;
            //            kk.InsertCTHD(cthdData);
            //        }
            //        Session["GioHang"] = null;
            //    }
            //}
            //else
            //    GridView1.DataBind();
        }

        protected void Label1_Load(object sender, EventArgs e)
        {
            Label lblSoLuong = (Label)sender;
            MyList list = (MyList)Session["GioHang"];
            foreach (CartItem item in list)
            {
                lblSoLuong.Text = item.Quantity.ToString();
            }
            //lblTong.Text = list.TongCart().ToString();
            //lblSoLuong.Text = list.SoLuong().ToString();
        }

        protected void Label2_Load(object sender, EventArgs e)
        {
            Label lblTong = (Label)sender;
            MyList list = (MyList)Session["GioHang"];
            lblTong.Text = list.TotalCart().ToString();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            MyList list = (MyList)Session["GioHang"];

            // If we are binding the footer row, let's add in our total
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[3].Text = "Total: " + list.TotalCart().ToString("#,###,### VND").Replace(",", ".");
                //Hàng triệu, trăm ngàn, hàng ngàn
            }
        }

        protected void btnUpdateCart_Click(object sender, EventArgs e)
        {
            MyList list = (MyList)Session["GioHang"];
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    // We'll use a try catch block in case something other than a number is typed in
                    // If so, we'll just ignore it.
                    try
                    {
                        // Get the productId from the GridView's datakeys
                        string productId = Convert.ToString(GridView1.DataKeys[row.RowIndex].Value);
                        // Find the quantity TextBox and retrieve the value
                        int quantity = int.Parse(((TextBox)row.Cells[4].FindControl("txtQuantity")).Text);

                        //TextBox t = (TextBox)r.Cells[2].FindControl("txtSoLuong");

                        list.UpdateCart(productId, quantity);
                    }
                    catch (FormatException) { }
                }
            }

            GridView1.DataSource = list;
            GridView1.DataBind();
        }

        protected void btnDeleteCart_Click(object sender, ImageClickEventArgs e)
        {
            LinkButton btn = (LinkButton)sender; // lấy nút phát sinh sự kiện
            string masp = btn.CommandArgument.ToString();
            MyList list = (MyList)Session["GioHang"];
            CartItem item = new CartItem();
            item.ProductID = masp;
            list.RemoveItem(masp);
            GridView1.DataSource = list;
            GridView1.DataBind();
        }
    }
}